"use client"
import { useState, useEffect } from 'react';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
// 引入中文本地化配置
import 'dayjs/locale/zh-cn';

// 扩展相对时间插件
dayjs.extend(relativeTime);
// 全局设置中文 locale
dayjs.locale('zh-cn');

// 获取相对时间
export const useRelativeTime = (dateString: string) => {
  const [relativeTime, setRelativeTime] = useState('');

  useEffect(() => {
    const updateTime = () => {
      const date = dayjs(dateString);
      setRelativeTime(date.isValid() ? date.fromNow() : '无效日期');
    };

    updateTime();
    const timer = setInterval(updateTime, 60000);
    
    return () => clearInterval(timer);
  }, [dateString]);

  return relativeTime;
};